//
//  LoginController.m
//  bitcoin
//
//  Created by caiyajun on 2018/4/25.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "LoginController.h"
#import "LoginSafeTableViewController.h"
@interface LoginController ()
@property (nonatomic, strong) NSString *rand;//图形验证码接口返回参数
@property (weak, nonatomic) IBOutlet UITextField *certifyTF;
@property (weak, nonatomic) IBOutlet UIImageView *certifyImg;
@property (weak, nonatomic) IBOutlet UIButton *changeBtn;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UITextField *usenameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *eyeBtn;
- (IBAction)changEyeAction:(UIButton *)sender;
- (IBAction)forgetAction;

- (IBAction)createAction;
- (IBAction)loginAction;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置头部
    self.headerView.frame = CGRectMake(0, 0, ScreenWidth, 230);
    if(iPhoneX){
        self.tableView.contentInset = UIEdgeInsetsMake(-88, 0, 0, 0);
    }else{
        self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    }
    [_usenameTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_passwordTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_certifyTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
//    _usenameTF.keyboardType = UIKeyboardTypeNumberPad;
    [_eyeBtn setImage:[UIImage imageNamed:@"open-icon"] forState:UIControlStateNormal];
    [_eyeBtn setImage:[UIImage imageNamed:@"close-icon"] forState:UIControlStateSelected];
    _eyeBtn.selected =YES;
    self.passwordTF.secureTextEntry =YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    [self.loginBtn addTarget:self action:@selector(didBtnColorChange:) forControlEvents:UIControlEventTouchDown];
    
    
    
    NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"换一个"];
    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,[tncString length]}];
    [tncString addAttribute:NSForegroundColorAttributeName value:RGBColor(123, 78, 225)  range:NSMakeRange(0,[tncString length])];
    //设置下划线颜色...
    [tncString addAttribute:NSUnderlineColorAttributeName value:RGBColor(123, 78, 225) range:(NSRange){0,[tncString length]}];
    [self.changeBtn setAttributedTitle:tncString forState:UIControlStateNormal];
    
    [self setCertifyImg];


}
- (IBAction)setCertifyImg {
    [LSRequestManager getRequestWithURL:HUOBI(@"api/pub/catchCaptcha")
                                 params:nil
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        NSString  *avatar=[NSString stringWithFormat:@"%@",responseData[@"data"][@"url"]];
                                        NSArray *array = [avatar componentsSeparatedByString:@","];
                                        //    base64转图形
                                        if (array.count>1) {
                                            NSData *imageData = [[NSData alloc] initWithBase64EncodedString:array[1] options:NSDataBase64DecodingIgnoreUnknownCharacters];
                                            
                                            [self.certifyImg setImage:[UIImage imageWithData:imageData]];
                                        }

                                        self.rand=[NSString stringWithFormat:@"%@",responseData[@"data"][@"rand"]];
                                        
                                    } else {
                                        self.certifyImg.image=nil;
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                    self.certifyImg.image=nil;
                                }
                                showHUD:NO];
}

-(void)didBtnColorChange:(UIButton *)securitycodeButton
{
    securitycodeButton.backgroundColor =RGBColor(86, 54, 157);
}
- (void)hideKeyboard{
    [self.view endEditing:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



- (IBAction)changEyeAction:(UIButton *)sender {
    self.passwordTF.enabled = NO;
    self.passwordTF.secureTextEntry = !sender.selected;
    sender.selected = !sender.selected;
    self.passwordTF.enabled = YES;
    
}

- (IBAction)forgetAction {
    
    [self performSegueWithIdentifier:@"forgetPasswordSegue" sender:nil];
    
}

- (IBAction)createAction {
    [self performSegueWithIdentifier:@"registerSegue" sender:nil];

}

- (IBAction)loginAction {
    //跳转到认证页面
//    [self performSegueWithIdentifier:@"loginAllsafeSegue" sender:nil];
    
    
    [self.view endEditing:YES];
    if (self.usenameTF.text.length==0) {
        [MBProgressHUD showError:@"请输入帐号"];
        return;
    }
    if (self.certifyTF.text.length==0) {
        [MBProgressHUD showError:@"请输入图形验证码"];
        return;
    }
    
    if ( ![CheckUtil validateEmail:self.usenameTF.text]) {
        [MBProgressHUD showError:@"请输入有效的邮箱地址!"];
        return;
    }
    

    NSDictionary *param = @{@"login_val" : self.usenameTF.text,
                            @"yzm" : self.certifyTF.text,
                            @"login_pwd" : self.passwordTF.text,
                            @"reg_type" : @"2",
                            @"rand" : self.rand,
                            };
    [LSRequestManager postRequestWithURL:HUOBI(@"api/auth/singin")
                                  params:param
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         
                                         if ([responseData[@"data"][@"token"] isEqualToString:@""]) {
                                             
                                             [UserDefaults setObject:[NSString stringWithFormat:@"%@",responseData[@"data"][@"security"][@"email"]] forKey:K_ISEMAIL];
                                             [UserDefaults setObject:[NSString stringWithFormat:@"%@",responseData[@"data"][@"security"][@"google"]] forKey:K_ISGOOGLE];
                                             [UserDefaults setObject:[NSString stringWithFormat:@"%@",responseData[@"data"][@"info"][@"mobile"]] forKey:K_MOBIL];
                                             [UserDefaults setObject:[NSString stringWithFormat:@"%@",responseData[@"data"][@"info"][@"email"]] forKey:K_EMAIL];
                                             [UserDefaults synchronize];
                                             
                                             
                                             [self performSegueWithIdentifier:@"loginAllsafeSegue" sender:nil];
                                         }else{
                                             [UserDefaults setObject:[NSString stringWithFormat:@"%@",responseData[@"data"][@"token"]] forKey:K_LOGININFO_TOKEN];
                                             [UserDefaults synchronize];
                                             
                                             [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                                             
                                         }
                                         
                                         
                                         
                                         
                                         
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
    
    
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"loginAllsafeSegue"]){//二次登陆安全验证
        
        LoginSafeTableViewController * loginSafeVC = segue.destinationViewController;
        loginSafeVC.pushType = @"login";
        
    }
}
@end
